/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets.admin;

import dao.BuyJpaController;
import dao.CartJpaController;
import dao.ColorsJpaController;
import dao.ImagesJpaController;
import dao.ProductJpaController;
import dao.SizesJpaController;
import dao.exceptions.IllegalOrphanException;
import dao.exceptions.NonexistentEntityException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.Buy;
import pojo.Cart;
import pojo.Colors;
import pojo.Images;
import pojo.Product;
import pojo.Sizes;

/**
 *
 * @author Dell
 */
public class admin_deleteProduct extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
          
            Integer pid = new Integer(request.getParameter("pid"));
           ProductJpaController p = new ProductJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
            SizesJpaController s = new SizesJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
            ColorsJpaController c = new ColorsJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
            ImagesJpaController i = new ImagesJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
            CartJpaController aa = new CartJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
            BuyJpaController ba = new BuyJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
           
            Product pp = new Product();         
            pp = p.findProduct(pid);
            
          
           
           if(pp.getBuyList().isEmpty())
           {
            
            List<Sizes> size = new ArrayList();            
            size = pp.getSizesList();           
            
            for(Sizes ss : size)
            s.destroy(ss.getId());
            
            
            
            List<Colors> cols = new ArrayList();
            cols = pp.getColorsList();
            
            for(Colors coll : cols)
            c.destroy(coll.getId());
            
            List<Images> imgs = new ArrayList();
            imgs = pp.getImagesList();
           
            for(Images img : imgs)
            i.destroy(img.getId());
            
            List<Cart> carts = new ArrayList();
            carts = pp.getCartList();
           
            for(Cart a : carts)
             aa.destroy(a.getCartPK());
            
              /*List<Buy> buys = new ArrayList();
            buys = pp.getBuyList();
            
            for(Buy ab : buys)
             ba.destroy(ab.getId()); */
            
           p.destroy(pp.getId());
          
           
           response.sendRedirect("admin_viewProducts.jsp");
           }
           else
           {
               HttpSession session = request.getSession();   
            session.setAttribute("producterror", "Cannot delete a product that was bought by a user.");
            response.sendRedirect("admin_viewProducts.jsp");
           }
           
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(admin_deleteProduct.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(admin_deleteProduct.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
            
            
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
